#!/usr/local/bin/ruby
# Copyright (C) 2003-2009 Wang Pengcheng <wpc0000@gmail.com>
# , released under the MIT license
# You may obtain a template of the MIT License at
# http://www.opensource.org/licenses/mit-license.php
$KCODE = 'utf8'

GS_TIP='Generate the getter and setter'
GS_FORMAT =<<FORMAT
  [ //  comment]
  visiable type var[;]
  visiable type var[;]
  ......
FORMAT
gs = Proc.new do |filename|
  filename ||='opt.txt'
  unless File.exist? filename
    puts filename+' NOT exists'
    puts GS_FORMAT
    exit
  end
  output = ''
  File.open(filename,'r') do |f|
    while not f.eof?
      line = f.readline
      if line=~ /\A\s*(\w+)\s+([\w<>]+)\s+(\w+);?/i
        data = Regexp.last_match  # => Get match details
        type =data[2]
        var = data[3]
        mvar = var[0].chr.upcase+var[1...var.length]
        output<<"\n"
        output<< 'public '+type+' get'+mvar+'()'+"\n"
        output<<"{\n"
        output<< '  return this.'+var+";\n"
        output<<"}\n\n"
        output<< 'public void set'+mvar+'('+type+' arg)'+"\n"
        output<<"{\n"
        output<< '  this.'+var+"=arg;\n"
        output<<"}\n"
      end
    end
  end
  puts output
end

opts={
  'gs'=>{:tip=>GS_TIP,:opt=>gs},
}
if ARGV.length<=0
  opts.each_pair do |k,v|
    puts k+' : '+v[:tip]
  end
  exit
end

opt, filename = ARGV
if opts[opt]
  puts opts[opt][:tip]
  opts[opt][:opt].call(filename)
else
  puts 'Error Operation:'+opt
end

